package leetcode.jianzhioffer;

import java.util.HashSet;
import java.util.Set;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-11-01 10:20
 **/
public class JianZhiOffer48 {

    public int lengthOfLongestSubstring(String s) {
        Set<Character> set = new HashSet<>();
        int len = s.length();
        int slow = 0;
        int fast = 0;
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < len; i++) {
            while (set.contains(s.charAt(i))) {
                set.remove(s.charAt(slow));
                slow++;
            }
            set.add(s.charAt(i));
            fast++;
            max = Math.max(max, fast - slow);
        }
        return max == Integer.MIN_VALUE ? 0 : max;
    }

}
